#  单元措施经济评价计算
import math


# 平均增油操作成本（元/井）：WellOprCost
#    平均措施增油（吨/井）：IncreOil
#    操作吨油成本（元/吨）：OilOprCost
def welloprcost(increoil, oiloprcost):
    WellOprCost = increoil * oiloprcost
    return WellOprCost


# 平均增油毛收入（元/井）：GrossSalesIncome
# 原油价格（元/吨）：OilPrice
# 原油商品率（%）：OilCommodityRatio
# 平均措施增油（吨/井）：IncreOil
def grosssalesincome(increoil, oilprice, oilcommodityratio):
    GrossSalesIncome = increoil * oilprice * oilcommodityratio / 100
    return GrossSalesIncome


# 平均销售税费（元/井）：TotalTax
#  平均增油毛收入（元/井）：GrossSalesIncome
#  原油销售总费率（%）：TotalTaxRatio
def totaltax(grosssalesincome, totaltaxratio):
    TotalTax = grosssalesincome * totaltaxratio / 100
    return TotalTax


# 平均静态收益（元/井）：StaticGains
# 平均作业成本（元/井）：StimCost
# 平均增油操作成本（元/井）：WellOprCost
# 平均销售税费（元/井）：TotalTax
# 平均增油毛收入（元/井）：GrossSalesIncome
# StaticGains = GrossSalesIncome - StimCost - WellOprCost - TotalTax
def staticgains(grosssalesincome, stimcost, welloprcost, totaltax):
    StaticGains = grosssalesincome - stimcost - welloprcost - totaltax
    return StaticGains


# 平均财务净现值（元 / 井）：StimNPV
# 有效期（天）：ValidPeriod
# ①有效年（年）：ValidYears = roundup(ValidPeriod / 365)，向上取整
# ②平均年增油（吨 /（年.井））：IncreOilPy = IncreOil / ValidYears
# ③平均年增油静态收益（元 /（年.井））：StaticGainsPy
# ④年贴现率（%）：Discount  年贴现率（小数）：ic=Discount/100
# ⑤平均财务净现值（元/井） StimNPV
def stimnpv(increoil, validperiod, oilcommodityratio, oilprice, totaltaxratio, oiloprcost, discount, stimcost):
    validyears = math.ceil(validperiod / 365)  # 向上取整
    increoilpy = increoil / validyears
    staticgainspy = increoilpy * (oilprice * oilcommodityratio / 100 * (1 - totaltaxratio / 100) - oiloprcost)
    ic = discount / 100
    data = 0
    for i in range(1, validyears + 1):
        data += (1 - ic) ** i
    StimNpv = data * staticgainspy - stimcost
    return StimNpv


# 投资利润率（%）：StimROI
# 均静态收益（元/井）：StaticGains
# 平均作业成本（元/井）：StimCost
def stimroi(staticgains, stimcost):
    StimROI = staticgains / stimcost * 100
    return StimROI


# 财务净现值率（%）：StimNPVR
# 平均财务净现值（元/井）：StimNPV
# 平均作业成本（元/井）：StimCost
def stimnpvr(stimnpv, stimcost):
    StimNPVRV = stimnpv / stimcost * 100
    return StimNPVRV


# 财务内部收益率（%）StimFIRR
def stimfirr(staticgains, stimcost, validperiod):
    validyears = math.ceil(validperiod / 365)  # 向上取整
    flag = stimcost / (staticgains * validyears)
    if flag >= 1:  # 说明收益为负
        firrmax = (1 - (flag ** validyears)) * 100
        firrmin = (1 - flag) * 100
    else:  # 收益为正
        firrmax = (1 - flag) * 100
        firrmin = (1 - (flag ** validyears)) * 100

    low = firrmin
    high = firrmax
    while low <= high:
        mid = math.ceil((low + high) / 2)
        data = 0
        for i in range(1, validyears + 1):
            data += (1 - mid) ** i
        if abs(data * staticgains - stimcost) < 0.001:
            return mid
        elif data * staticgains - stimcost > 0.001:
            low = mid
        else:
            high = mid
    return -1  # 说明计算出现错误


# 平均经济临界增油量（吨/井）：IncreOilCritical
def increoilcritical(validperiod, stimcost, discount, oilprice, oilcommodityratio, totaltaxratio, oiloprcost):
    validyears = math.ceil(validperiod / 365)  # 向上取整
    # StaticGainsPyCritical
    ic = discount / 100
    data = 0
    for i in range(1, validyears + 1):
        data += (1 - ic) ** i
    staticgainspycritical = stimcost / data
    IncreOilPyCritical = staticgainspycritical / (
                oilprice * oilcommodityratio / 100 * (1 - totaltaxratio / 100) - oiloprcost)
    IncreOilCritical = IncreOilPyCritical * validyears
    return IncreOilCritical


# 平均经济临界油价（元/吨）：OilPriceCritical
def oilpricecritical(discount, validperiod, stimcost, increoil, oiloprcost, oilcommodityratio, totaltaxratio):
    validyears = math.ceil(validperiod / 365)  # 向上取整
    ic = discount / 100
    data = 0
    for i in range(1, validyears + 1):
        data += (1 - ic) ** i
    staticgainspycritical = stimcost / data
    increoilpy = increoil / validyears
    OilPriceCritical = (staticgainspycritical / increoilpy + oiloprcost) / (
                oilcommodityratio / 100 * (1 - totaltaxratio / 100))
    return OilPriceCritical


# 计算销售税费率
def totaltax_ratio(addedvalue_taxratio, resource_taxratio, eduadd_taxratio, citydvpadd_taxrate, income_taxrate,
                   managecost_ratio):
    return (
            addedvalue_taxratio + resource_taxratio + eduadd_taxratio + citydvpadd_taxrate + income_taxrate + managecost_ratio)


# 计算操作吨油成本
def oiloprcost(fuelcost, power_cost, fluiddispose_cost, oildisplaceagent_cost, nglrecovery_cost, transp_cost,
               welltest_cost, other_cost, depre_cost):
    return (
            fuelcost + power_cost + fluiddispose_cost + oildisplaceagent_cost + nglrecovery_cost + transp_cost +
            welltest_cost + other_cost + depre_cost)
